package cn.school.mybatis.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import cn.school.mybatis.Dao.ExamResultDao;
import cn.school.mybatis.join.ExamResult;
import cn.school.mybatis.xml.MyBatisUtil;

public class ExamResultDaoTest {
	public static void main(String[] args) {
		get();
		// update();
		// del();
		// add();
	}

	// 查询单个
	// 作业三完成查询成绩的同时获得科目和学生信息
	public static void get() {
		SqlSession session = MyBatisUtil.get();
		ExamResultDao examResultDao = session.getMapper(ExamResultDao.class);
		ExamResult e = examResultDao.get(13);
		System.out.println("成绩为" + e);
		System.out.println("科目为" + e.getSubjectName());
		System.out.println("学生信息为:" + e.getStu());
		MyBatisUtil.close(session);
	}

	// 修改
	public static void update() {
		SqlSession session = MyBatisUtil.get();
		ExamResultDao examResultDao = session.getMapper(ExamResultDao.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		int number = 0;
		try {
			number = examResultDao.update(new ExamResult(2, sdf.parse("2016-10-22"), 98, 5, 34));
		} catch (ParseException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			MyBatisUtil.close(session);
		}
		System.out.println(number);
	}

	// 删除
	public static void del() {
		SqlSession session = MyBatisUtil.get();
		ExamResultDao examResultDao = session.getMapper(ExamResultDao.class);
		int number = examResultDao.del(34);
		System.out.println(number);
		MyBatisUtil.close(session);
	}

	// 增加
	public static void add() {
		SqlSession session = MyBatisUtil.get();
		ExamResultDao examResultDao = session.getMapper(ExamResultDao.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map.put("examDate", sdf.parse("2016-10-22"));
			map.put("score", 90);
			map.put("stId", 10);
			map.put("sbId", 18);
			map.put("id", 35);
		} catch (ParseException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		int number = examResultDao.add(map);
		System.out.println(number);
		MyBatisUtil.close(session);

	}
}
